/*
 * Copyright (c) 2024 iSoftStone Education Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "smart_security.h"

#include <stdbool.h>
#include <stdio.h>

#include "lcd.h"
#include "mq2.h"

#include "iot_pwm.h"
#include "iot_gpio.h"

#define BEEP_PORT EPWMDEV_PWM5_M0
#define GPIO_ALARM_LIGHT GPIO0_PA5
#define GPIO_BODY_INDUCTION GPIO0_PA3

static bool body_induction_state = false;

/***************************************************************
 * 函数名称: mq2_init
 * 说    明: mq2初始化
 * 参    数: 无
 * 返 回 值: 无
 ***************************************************************/
void mq2_init(void)
{
    mq2_dev_init();
    mq2_ppm_calibration();
}

/***************************************************************
 * 函数名称: mq2_read_data
 * 说    明: 读取mq2传感器数据
 * 参    数: 无
 * 返 回 值: 无
 ***************************************************************/
void mq2_read_data(double *dat)
{
    *dat = get_mq2_ppm();
}

/***************************************************************
 * 函数名称: beep_dev_init
 * 说    明: 蜂鸣器初始化
 * 参    数: 无
 * 返 回 值: 无
 ***************************************************************/
void beep_dev_init(void)
{
    IoTPwmInit(BEEP_PORT);
}

/***************************************************************
 * 函数名称: beep_set_pwm
 * 说    明: 设置蜂鸣器PWM
 * 参    数: 无
 * 返 回 值: 无
 ***************************************************************/
void beep_set_pwm(unsigned int duty)
{
    IoTPwmStart(BEEP_PORT, duty, 1000);
}

/***************************************************************
 * 函数名称: beep_set_state
 * 说    明: 设置蜂鸣器状态
 * 参    数: 无
 * 返 回 值: 无
 ***************************************************************/
void beep_set_state(bool state)
{
    static bool last_state = false;

    if (state == last_state)
    {
        return;
    }

    if (state)
    {
        beep_set_pwm(20);
    }
    else
    {
        beep_set_pwm(1);
        IoTPwmStop(BEEP_PORT);
    }

    last_state = state;
}

/***************************************************************
 * 函数名称: alarm_light_init
 * 说    明: 报警灯初始化
 * 参    数: 无
 * 返 回 值: 无
 ***************************************************************/
void alarm_light_init(void)
{
    IoTGpioInit(GPIO_ALARM_LIGHT);
    IoTGpioSetDir(GPIO_ALARM_LIGHT, IOT_GPIO_DIR_OUT);
}

/***************************************************************
 * 函数名称: alarm_light_set_gpio
 * 说    明: 设置报警灯
 * 参    数: 无
 * 返 回 值: 无
 ***************************************************************/
void alarm_light_set_gpio(bool state)
{
    static bool last_state = false;

    if (state == last_state)
    {
        return;
    }

    if (state)
    {
        IoTGpioSetOutputVal(GPIO_ALARM_LIGHT, IOT_GPIO_VALUE1);
    }
    else
    {
        IoTGpioSetOutputVal(GPIO_ALARM_LIGHT, IOT_GPIO_VALUE0);
    }

    last_state = state;
}

/***************************************************************
 * 函数名称: body_induction_get_state
 * 说    明: 获取人体感应状态
 * 参    数: 无
 * 返 回 值: 无
 ***************************************************************/
void body_induction_get_state(bool *dat)
{
    IotGpioValue value = IOT_GPIO_VALUE0;

    IoTGpioGetInputVal(GPIO_BODY_INDUCTION, &value);

    if (value) 
    {
        *dat = true;
    }
    else
    {
        *dat = false;
    }
}

/***************************************************************
 * 函数名称: body_induction_dev_init
 * 说    明: 人体感应传感器初始化
 * 参    数: 无
 * 返 回 值: 无
 ***************************************************************/
void body_induction_dev_init(void)
{
    IoTGpioInit(GPIO_BODY_INDUCTION);
    IoTGpioSetDir(GPIO_BODY_INDUCTION, IOT_GPIO_DIR_IN);
}

/***************************************************************
 * 函数名称: lcd_dev_init
 * 说    明: lcd初始化
 * 参    数: 无
 * 返 回 值: 无
 ***************************************************************/
void lcd_dev_init(void)
{
    lcd_init();
    lcd_fill(0, 0, LCD_W, LCD_H, LCD_WHITE);
}

/***************************************************************
 * 函数名称: lcd_load_ui
 * 说    明: lcd加载ui
 * 参    数: 无
 * 返 回 值: 无
 ***************************************************************/
void lcd_load_ui(void)
{

    lcd_show_chinese(40, 20, "空间站监测系统", LCD_RED, LCD_WHITE, 32, 0);
    // 可燃气体和报警灯状态
    lcd_show_chinese(0, 60, "可燃气体", LCD_RED, LCD_WHITE, 16, 0);
    //lcd_show_string(65, 60, ":", LCD_RED, LCD_WHITE, 16, 0);

    lcd_show_chinese(0, 160, "报警灯状态", LCD_RED, LCD_WHITE, 16, 0); // 0,160.80,160
   lcd_show_string(80, 160, ":", LCD_RED, LCD_WHITE, 16, 0);

    lcd_show_chinese(0, 100, "电机状态", LCD_RED, LCD_WHITE, 16, 0);
    lcd_show_string(65, 100, ":", LCD_RED, LCD_WHITE, 16, 0);

    lcd_show_chinese(0, 120, "舵机状态", LCD_RED, LCD_WHITE, 16, 0);
    lcd_show_string(65, 120, ":", LCD_RED, LCD_WHITE, 16, 0);

    lcd_show_chinese(0, 140, "蜂鸣器状态", LCD_RED, LCD_WHITE, 16, 0);
    lcd_show_string(80, 140, ":", LCD_RED, LCD_WHITE, 16, 0);

    lcd_show_chinese(0, 80, "是否有人", LCD_RED, LCD_WHITE, 16, 0); // 0,80,65,80
    lcd_show_string(65, 80, ":", LCD_RED, LCD_WHITE, 16, 0);
    // 温湿度相关
    lcd_show_chinese(170, 60, "温度", LCD_RED, LCD_WHITE, 16, 0);
    //lcd_show_string(200, 60, ":", LCD_RED, LCD_WHITE, 16, 0);
    lcd_show_chinese(170, 80, "湿度", LCD_RED, LCD_WHITE, 16, 0);
   // lcd_show_string(200, 80, ":", LCD_RED, LCD_WHITE, 16, 0);

    // 亮度和灯状态
    lcd_show_chinese(170, 100, "亮度", LCD_RED, LCD_WHITE, 16, 0);
    //lcd_show_string(200, 100, ":", LCD_RED, LCD_WHITE, 16, 0);

    lcd_show_string(0, 180, "RGB", LCD_RED, LCD_WHITE, 16, 0);
    lcd_show_chinese(25, 180, "灯状态", LCD_RED, LCD_WHITE, 16, 0);
   lcd_show_string(75, 180, ":", LCD_RED, LCD_WHITE, 16, 0);

    // 加速度显示
    lcd_show_chinese(170, 120, "加速度", LCD_RED, LCD_WHITE, 16, 0);
    lcd_show_string(218, 120, "X", LCD_RED, LCD_WHITE, 16, 0);
    //lcd_show_string(225, 120, ":", LCD_RED, LCD_WHITE, 16, 0);
    lcd_show_chinese(170, 140, "加速度", LCD_RED, LCD_WHITE, 16, 0);
    lcd_show_string(218, 140, "Y", LCD_RED, LCD_WHITE, 16, 0);
   // lcd_show_string(225, 140, ":", LCD_RED, LCD_WHITE, 16, 0);
    lcd_show_chinese(170, 160, "加速度", LCD_RED, LCD_WHITE, 16, 0);
    lcd_show_string(218, 160, "Z", LCD_RED, LCD_WHITE, 16, 0);
    //lcd_show_string(225, 160, ":", LCD_RED, LCD_WHITE, 16, 0);

  
}



void lcd_set_temperature(double temperature)
{
    uint8_t buf[50] = {0};

    sprintf(buf, ": %.2f C", temperature);

    lcd_show_string(200, 60, buf, LCD_RED, LCD_WHITE, 16, 0);
}


void lcd_set_humidity(double humidity)
{
    uint8_t buf[50] = {0};

    sprintf(buf, ": %.2f RH", humidity);

    lcd_show_string(200, 80, buf, LCD_RED, LCD_WHITE, 16, 0);
}


void lcd_set_ppm(double ppm)
{
    uint8_t buf[50] = {0};

    sprintf(buf, ": %.2f ppm", ppm);

    lcd_show_string(65, 60, buf, LCD_RED, LCD_WHITE, 16, 0);
}


void lcd_set_illumination(double illumination)
{
    uint8_t buf[50] = {0};

    sprintf(buf, ": %.2f lx", illumination);

    lcd_show_string(200, 100, buf, LCD_RED, LCD_WHITE, 16, 0);
}



void lcd_set_body_induction(bool body_induction)
{
    if (body_induction)
    {
        lcd_show_chinese(75, 80, "有人", LCD_RED, LCD_WHITE, 16, 0);
    }
    else
    {
        lcd_show_chinese(75, 80, "无人", LCD_RED, LCD_WHITE, 16, 0);
    }
}


void lcd_set_mortor_state(bool state)
{
    if (state)
    {
        lcd_show_chinese(75, 100, "开启", LCD_RED, LCD_WHITE, 16, 0);
    }
    else
    {
        lcd_show_chinese(75, 100, "关闭", LCD_RED, LCD_WHITE, 16, 0);
    }
}

void lcd_set_mg90_state(bool state)
{
    if (state)
    {
        lcd_show_chinese(75, 120, "开启", LCD_RED, LCD_WHITE, 16, 0);
    }
    else
    {
        lcd_show_chinese(75, 120, "关闭", LCD_RED, LCD_WHITE, 16, 0);
    }
}


void lcd_set_RGB_state(bool state)
{
    if (state)
    {
        lcd_show_chinese(85, 180, "开启", LCD_RED, LCD_WHITE, 16, 0);
    }
    else
    {
        lcd_show_chinese(85, 180, "关闭", LCD_RED, LCD_WHITE, 16, 0);
    }
}



void lcd_set_redled_state(bool state)
{
    if (state)
    {
        lcd_show_chinese(90, 160, "开启", LCD_RED, LCD_WHITE, 16, 0);
    }
    else
    {
        lcd_show_chinese(90, 160, "关闭", LCD_RED, LCD_WHITE, 16, 0);
    }
}

void lcd_set_beep_state(bool state)
{
    if (state)
    {
        lcd_show_chinese(90, 140, "开启", LCD_RED, LCD_WHITE, 16, 0);
    }
    else
    {
        lcd_show_chinese(90, 140, "关闭", LCD_RED, LCD_WHITE, 16, 0);
    }
}



void lcd_set_SpeedX(short state)
{
    uint8_t buf[50] = {0};

    sprintf(buf, ": %d ", state);

    lcd_show_string(225, 120, buf, LCD_RED, LCD_WHITE, 16, 0);
}

void lcd_set_SpeedY(short state)
{
    uint8_t buf[50] = {0};

    sprintf(buf, ": %d ", state);

    lcd_show_string(225, 140, buf, LCD_RED, LCD_WHITE, 16, 0);
}

void lcd_set_SpeedZ(short state)
{
    uint8_t buf[50] = {0};

    sprintf(buf, ": %d ", state);

    lcd_show_string(225, 160, buf, LCD_RED, LCD_WHITE, 16, 0);
}


// /***************************************************************
//  * 函数名称: lcd_set_ppm
//  * 说    明: lcd设置烟雾浓度显示
//  * 参    数: 无
//  * 返 回 值: 无
//  ***************************************************************/
// void lcd_set_ppm(double ppm)
// {
//     uint8_t buf[50] = {0};

//     sprintf(buf, ": %.2f ", ppm);

//     lcd_show_string(101, 58, buf, LCD_RED, LCD_WHITE, 24, 0);
// }

// /***************************************************************
//  * 函数名称: lcd_set_body_induction
//  * 说    明: lcd设置人体感应显示
//  * 参    数: 无
//  * 返 回 值: 无
//  ***************************************************************/
// void lcd_set_body_induction(bool body_induction)
// {
//     if (body_induction)
//     {
//         lcd_show_chinese(125, 82, "有人", LCD_RED, LCD_WHITE, 24, 0);
//     }
//     else
//     {
//         lcd_show_chinese(125, 82, "无人", LCD_RED, LCD_WHITE, 24, 0);
//     }
// }

// /***************************************************************
//  * 函数名称: lcd_set_beep_state
//  * 说    明: lcd设置蜂鸣器状态
//  * 参    数: 无
//  * 返 回 值: 无
//  ***************************************************************/
// void lcd_set_beep_state(bool state)
// {
//     if (state)
//     {
//         lcd_show_chinese(101, 156, "开启", LCD_RED, LCD_WHITE, 24, 0);
//     }
//     else
//     {
//         lcd_show_chinese(101, 156, "关闭", LCD_RED, LCD_WHITE, 24, 0);
//     }
// }

// /***************************************************************
//  * 函数名称: lcd_set_alarm_light_state
//  * 说    明: lcd设置警报灯状态
//  * 参    数: 无
//  * 返 回 值: 无
//  ***************************************************************/
// void lcd_set_alarm_light_state(bool state)
// {
//     if (state)
//     {
//         lcd_show_chinese(101, 180, "开启", LCD_RED, LCD_WHITE, 24, 0);
//     }
//     else
//     {
//         lcd_show_chinese(101, 180, "关闭", LCD_RED, LCD_WHITE, 24, 0);
//     }
// }

// /***************************************************************
//  * 函数名称: lcd_set_auto_state
//  * 说    明: lcd设置自动模式状态
//  * 参    数: 无
//  * 返 回 值: 无
//  ***************************************************************/
// void lcd_set_auto_state(bool state)
// {
//     if (state)
//     {
//         lcd_show_chinese(101, 204, "开启", LCD_RED, LCD_WHITE, 24, 0);
//     }
//     else
//     {
//         lcd_show_chinese(101, 204, "关闭", LCD_RED, LCD_WHITE, 24, 0);
//     }
// }
